package com.quik.web.user.mapper;

import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import com.quik.backend.mapper.BaseDao;
import com.quik.entity.SysAccount;
import com.quik.entity.SysRoleRight;
import com.quik.web.base.*;

/**
 * 功能说明：
 * 		帐户管理数据持久化类
 * 
 * @author jenny
 *
 */
@Repository
public class AccountDao extends BaseDao<SysAccount>{
	
	/**
	 * 根据员工编号或登录查询激活用户
	 * @param loginName
	 * @return
	 * @throws Exception
	 */
	public SysAccount getAccountByStaffNoOrLoginName(String loginName) throws Exception{
		List<SysAccount> aList = (List<SysAccount>)load("getAccountByStaffNoOrLoginName", loginName);
		if(aList != null && aList.size() == 1)
			return aList.get(0);
		else
			return null;
	}
	
	/**
	 * 新增用户
	 * @param account
	 * @throws Exception
	 */
	public void addUser(SysAccount account) throws Exception{
		add("addUser", account);
	}
	
	/**
	 * 根据工号查询用户数据
	 * @param staffNo
	 * @return
	 * @throws Exception
	 */
	public int findAccountCountByStaffNo(String staffNo) throws Exception{
		int count = (Integer)this.get("findAccountCountByStaffNo", (Object)staffNo);
		return count;
	}
	
	/**
	 * 根据登录名查询用户数
	 * @param staffNo
	 * @return
	 * @throws Exception
	 */
	public int findAccountCountByLoginName(String loginName) throws Exception{
		int count = (Integer)this.get("findAccountCountByLoginName", (Object)loginName);
		return count;
	}
	
	/**
	 * 根据查询条件查询用户总数
	 * @param staffNo
	 * @param loginName
	 * @return
	 * @throws Exception
	 */
	public int findUserCount(Integer userType, String staffNo, String loginName) throws Exception{
		Map map = new HashMap(3);
		map.put("userType", userType);
		map.put("staffNo", staffNo);
		map.put("loginName", loginName);
		
		int count = (Integer)this.get("findUserCount", map);
		
		return count;
	}
	
	/**
	 * 根据条件查询用户列表
	 * @param staffNo
	 * @param loginName
	 * @param page
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	public List<SysAccount> findUser(Integer userType, String staffNo, String loginName, Integer page, Integer pageSize) throws Exception{
		Map map = new HashMap(5);
		map.put("staffNo", staffNo == null?null:staffNo.trim());		
		map.put("loginName", loginName == null?null:loginName.trim());
		map.put("userType", userType);
		map.put("currPage", (page - 1) * pageSize);
		map.put("pageSize", pageSize);
		
		List<SysAccount> list = load("findUser", map);
		
		return list;
	}
	public List<SysRoleRight> findRoleRightByUser(SysAccount user){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", user.getId());
		return load("findRoleRight", params);
	}
}
